package com.tadpole.music.view.activity.login;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.tadpole.music.R;
import com.tadpole.music.bean.WeChatUserInfoBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.login.SendCodeIView;
import com.tadpole.music.iView.wechat.WeChatBindPhone;
import com.tadpole.music.presenter.login.SendCodePresenter;
import com.tadpole.music.presenter.wechat.WeChatBindPhonePresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.StatusBarUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.MainActivity;
import com.tadpole.music.view.activity.base.BaseActivity;

/**
 * 微信绑定手机
 * Created By 孙岩
 * on 2020/12/4
 */
public class BindPhoneActivity extends BaseActivity implements SendCodeIView , WeChatBindPhone {
    //回退
    private View view_back_icon;
    //手机号
    private EditText etPhone;
    //验证码
    private EditText etCode;
    //发送验证码
    private TextView tvSendCode;
    //绑定
    private TextView tvBind;
    //是否发送了验证码
    private boolean isSendCode = false;
    //倒计时
    private TimeCount time;
    //jPushId
    private String jPushId;
    //微信返回的数据
    private WeChatUserInfoBean bean;

    //发送验证码
    private SendCodePresenter sendCodePresenter;
    //微信绑定手机号
    private WeChatBindPhonePresenter weChatBindPhonePresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_phone);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //将头部状态栏置为透明
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        bean = (WeChatUserInfoBean) getIntent().getSerializableExtra("info");
        Bundle bundle = this.getIntent().getExtras();
        jPushId = bundle.getString("jPushId");

        time = new TimeCount(60000,1000);
        initViews();
        initListeners();

        sendCodePresenter = new SendCodePresenter();
        sendCodePresenter.attachView(this);

        weChatBindPhonePresenter = new WeChatBindPhonePresenter();
        weChatBindPhonePresenter.attachView(this);
    }

    //初始化控件
    private void initViews(){
        view_back_icon = findViewById(R.id.view_back_icon);
        etPhone = findViewById(R.id.etPhone);
        etCode = findViewById(R.id.etCode);
        tvSendCode = findViewById(R.id.tvSendCode);
        tvBind = findViewById(R.id.tvBind);
    }

    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                BindPhoneActivity.this.finish();
            }
        });

        //发送验证码
        tvSendCode.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    if(etPhone.getText().toString().trim().equals("")){
                        ToastUtils.show("请输入手机号");
                    }else{
                        sendCodePresenter.sendCode(etPhone.getText().toString().trim());
                        time.start();
                    }
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //绑定
        tvBind.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    if(etPhone.getText().toString().trim().equals("")){
                        ToastUtils.show("请输入手机号");
                    }else if(!isSendCode){
                        ToastUtils.show("请先发送验证码");
                    }else if(etCode.getText().toString().trim().equals("")){
                        ToastUtils.show("请输入验证码");
                    }else{
                        weChatBindPhonePresenter.bindPhone(
                                bean.getOpenid(),
                                bean.getUnionid(),
                                bean.getNickname(),
                                bean.getCountry(),
                                bean.getLanguage(),
                                bean.getProvince(),
                                bean.getCity(),
                                bean.getHeadimgurl(),
                                bean.getSex(),
                                jPushId,
                                etPhone.getText().toString().trim(),
                                etCode.getText().toString().trim()
                        );
                    }
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    @Override
    public void showCode() {
        isSendCode = true;
    }

    @Override
    public void showToken(String token) {
        ToastUtils.show("登录成功");
        SpUtil.getInstance(this).putString(Constant.TOKEN, "Bearer "+ token);

        Intent intent = new Intent(BindPhoneActivity.this, MainActivity.class);
        startActivity(intent);
        this.finish();
    }

    @Override
    public void bindPhone() {

    }

    //倒计时
    class TimeCount extends CountDownTimer {
        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tvSendCode.setText(millisUntilFinished / 1000 + "s后重新获取");
            tvSendCode.setClickable(false);
        }

        @Override
        public void onFinish() {
            tvSendCode.setText("获取验证码");
            tvSendCode.setClickable(true);
        }
    }
}
